<template>
  <el-card>
    <div id="bar-chart" style="width: 100%; height: 400px" />
  </el-card>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'Barchart',
  props: {
    articleAxis: {
      type: Array,
      required: true
    },
    barData: {
      type: Array,
      required: true
    }

  },
  data() {
    return {
      myChart: null
    }
  },
  computed: {
    echarsetConfig() {
      return {
        title: {
          text: '文章浏览量排行',
          textStyle: {
            color: '#767676'
          },
          left: 'left'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        toolbox: {
          feature: {
            saveAsImage: {},
            restore: {},
            dataView: {},
            // dataZoom: {},
            magicType: {
              type: ['line', 'bar']
            }
          }
        },
        grid: {
          left: '5%',
          right: '3%',
          bottom: '10%'
        },
        xAxis: {
          type: 'category',
          data: this.articleAxis
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: this.barData,
            type: 'bar',
            smooth: true
          }
        ]
      }
    }
  },
  watch: {
    articleAxis(oldValue, newValues) {
      this.myChart.setOption(this.echarsetConfig)
    }
  },
  mounted() {
    this.initCharset()
  },
  methods: {
    initCharset() {
      this.$nextTick(() => {
        this.myChart = echarts.init(document.getElementById('bar-chart'))
        this.myChart.setOption(this.echarsetConfig)
      })
    }
  }
}
</script>

<style scoped>

</style>
